/**

* Definition for singly-linked list.

* struct ListNode {

* int val;

* ListNode *next;

* ListNode(int x) : val(x), next(NULL) {}

* };

*/

class Solution {

public:

ListNode\* removeElements\(ListNode\* head, int val\) {

    ListNode\* result=\(ListNode\*\)malloc\(sizeof\(ListNode\)\);



    if \(head==NULL\) return NULL;

    if \(head->next==NULL\){

        if\(head->val==val\){

            return NULL;

        }

        else{

            return head;

        }

    }



    result=head;

    // remove iterately

    while\(head->next!=NULL\){

       // printf\("%d %d\n",result->val,head->val\);

        if \(head->next->val==val\){

            head->next=head->next->next;

        }

        else{

            head=head->next;

        }

    }

    if \(result->val==val\){

        result=result->next;

    }

    else{

        result=result;

    }

    return result;

}

};

results matching ""

    No results matching ""